﻿using Abp.Domain.Entities;
using ARchGL.Platform.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace ARchGL.Platform.EntityFrameworkCore
{
    public static class PlatformDbContextExtensions
    {
        #region 加载插件中的实体类型

        public static void AddEntityFromPlugins(this TenantDbContext context, ModelBuilder modelBuilder)
        {
            var pluginPaths = FindPlugins();
            foreach (string path in pluginPaths)
            {
                try
                {
                    var typesToRegister = Assembly.LoadFile(path).GetTypes();
                    //var typesToRegister = Assembly.LoadFrom(path).GetTypes();

                    context.AddEntityFromTypes(modelBuilder, typesToRegister);

                    //ApplyEntityConfiguration(modelBuilder, typesToRegister);

                    //ApplyEntityType(context, modelBuilder, typesToRegister);
                }
                catch (Exception)
                {
                    continue;
                }
            }
        }

        public static List<string> FindPlugins()
        {
            var pluginPaths = new List<string>();
            try
            {
                var path = WebContentDirectoryFinder.GetContentRootFolder();
                path = Path.Combine(path, "wwwroot", "Plugins");
                var fileNames = Directory.GetFiles(path, "*.dll");
                foreach (var fileName in fileNames)
                {
                    pluginPaths.Add(fileName);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return pluginPaths;
        }

        public static void AddEntityFromTypes(this TenantDbContext context, ModelBuilder modelBuilder, Type[] types)
        {
            ApplyEntityConfiguration(modelBuilder, types);
            ApplyEntityType(context, modelBuilder, types);
        }

        private static void ApplyEntityType(DbContext context, ModelBuilder modelBuilder, Type[] types)
        {
            var typesToRegister = types
                                .Where(type => !string.IsNullOrWhiteSpace(type.Namespace))
                                .Where(type => !type.IsAbstract && type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition().IsAssignableFrom(typeof(IEntity<>))));

            foreach (var type in typesToRegister)
            {
                if (modelBuilder.Model.FindEntityType(type) != null)
                {
                    continue;
                }

                modelBuilder.Model.AddEntityType(type);
            }
        }

        private static void ApplyEntityConfiguration(ModelBuilder modelBuilder, Type[] types)
        {
            var typesToRegister = types
                .Where(type => !string.IsNullOrWhiteSpace(type.Namespace))
                .Where(type => type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition().IsAssignableFrom(typeof(IEntityTypeConfiguration<>))));

            foreach (var type in typesToRegister)
            {
                dynamic instance = Activator.CreateInstance(type);
                modelBuilder.ApplyConfiguration(instance);
            }
        }

        #endregion
    }
}
